package com.sample.jdbc;

import java.sql.*;
import java.sql.DriverManager;

public class JdbcUtils {
/**
 * 数据库连接
 * @return
 */
	public Connection getConn(){
		Connection conn=null;
		try{

			 Class.forName("com.mysql.jdbc.Driver");

			 String url="jdbc:mysql://127.0.0.1:3306/mxb?characterEncoding=utf-8";
			 String user="root";
			 String password="root";

		    conn =DriverManager.getConnection(url, user, password);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	/**
	 * 
	 * @param conn
	 * @param stmt
	 * @param rs
	 */
	public void close(Connection conn,Statement stmt,ResultSet rs){
		try{
		if(rs!=null){
			rs.close();
		}
		if(stmt!=null){
			stmt.close();
		}
		if(conn!=null){
			conn.close();
		}
		}catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	public void close(Connection conn,Statement stmt){
		close(conn, stmt, null);
		
	}
	
	public static void main(String[]args){
		try{
	
			JdbcUtils jdbcUtils=new JdbcUtils();
			Connection conn=jdbcUtils.getConn();
			System.out.println("连接对象是否为null?"+conn.getCatalog());
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
